
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        queue.offer(4);
        queue.offer(5);
        System.out.println(queue.peek());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.peek());
        if(queue.isEmpty()){
            System.out.println("队列空");
        }else{
            System.out.println(queue.size());
        }
    }
    public static void main4(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        System.out.println(myQueue.peek());
        System.out.println(myQueue.poll());
    }
    //用单链表实现栈
    public static void main3(String[] args) {
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }

    //出栈入栈次序匹配
    public boolean isPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while(j < popA.length && !stack.empty() && stack.peek().equals(popA[j])) {
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }



    //括号匹配
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                //遇到了右括号
                if(stack.empty()) {
                    return false;//右括号多
                }
                char ch2 = stack.peek();
                if(ch == ')' && ch2 == '(' || ch == '}' && ch2 == '{' || ch == ']' && ch2 =='[') {
                    stack.pop();
                }else {
                    return false;//不匹配了
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }



    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if(!isOperation(x)) {
                stack.push(Integer.parseInt(x));
            }else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (x) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public boolean isOperation(String x) {
        if(x.equals("+") || x.equals("-") || x.equals("*") || x.equals("/")) {
            return true;
        }
        return false;
    }
    public static void main2(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);//压栈
        stack.push(2);
        stack.push(3);
        stack.push(4);
        Integer a = stack.pop();//出栈
        System.out.println(a);
        Integer b = stack.peek();
        System.out.println(b);
        Integer b2 = stack.peek();
        System.out.println(b2);
        System.out.println(stack.isEmpty());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);//压栈
        stack.push(2);
        stack.push(3);
        stack.push(4);
        Integer a = stack.pop();//出栈
        System.out.println(a);
        Integer b = stack.peek();
        System.out.println(b);
        Integer b2 = stack.peek();
        System.out.println(b2);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());
    }
}
